package com.TocaLetras;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {
	 
	static private SoundManager _instance;
	private static SoundPool mSoundPool;
	private static HashMap mSoundPoolMap;
	private static AudioManager  mAudioManager;
	private static Context mContext;
 
	private SoundManager()
	{
	}
 
	/**
	 * Requests the instance of the Sound Manager and creates it
	 * if it does not exist.
	 *
	 * @return Returns the single instance of the SoundManager
	 */
	static synchronized public SoundManager getInstance()
	{
	    if (_instance == null)
	      _instance = new SoundManager();
	    return _instance;
	 }
 
	/**
	 * Initialises the storage for the sounds
	 *
	 * @param theContext The Application context
	 */
	public static  void initSounds(Context theContext)
	{
		 mContext = theContext;
	     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
	     mSoundPoolMap = new HashMap();
	     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
	} 
 
	/**
	 * Add a new Sound to the SoundPool
	 *
	 * @param Index - The Sound Index for Retrieval
	 * @param SoundID - The Android ID for the Sound asset.
	 */
	public static void addSound(int Index,int SoundID)
	{
		mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
	}
 
	/**
	 * Loads the various sound assets
	 * Currently hardcoded but could easily be changed to be flexible.
	 */
	public static void loadSounds()
	{
		mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.agua, 1));
		mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.auto, 1));
		mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.barco, 1));
		mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.bien, 1));
		mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.carro, 1));
		mSoundPoolMap.put(6, mSoundPool.load(mContext, R.raw.casa, 1));
		mSoundPoolMap.put(7, mSoundPool.load(mContext, R.raw.cuak, 1));
		mSoundPoolMap.put(8, mSoundPool.load(mContext, R.raw.dados, 1));
		mSoundPoolMap.put(9, mSoundPool.load(mContext, R.raw.elefante, 1));
		mSoundPoolMap.put(10, mSoundPool.load(mContext, R.raw.elephantsound, 1));
		mSoundPoolMap.put(11, mSoundPool.load(mContext, R.raw.foca, 1));
		mSoundPoolMap.put(12, mSoundPool.load(mContext, R.raw.grillos, 1));
		mSoundPoolMap.put(13, mSoundPool.load(mContext, R.raw.helado, 1));
		mSoundPoolMap.put(14, mSoundPool.load(mContext, R.raw.isla, 1));
		mSoundPoolMap.put(15, mSoundPool.load(mContext, R.raw.island, 1));
		mSoundPoolMap.put(16, mSoundPool.load(mContext, R.raw.jirafa, 1));
		mSoundPoolMap.put(17, mSoundPool.load(mContext, R.raw.kilogramo, 1));
		mSoundPoolMap.put(18, mSoundPool.load(mContext, R.raw.luna, 1));
		mSoundPoolMap.put(19, mSoundPool.load(mContext, R.raw.mal, 1));
		mSoundPoolMap.put(20, mSoundPool.load(mContext, R.raw.mama, 1));
		mSoundPoolMap.put(21, mSoundPool.load(mContext, R.raw.nube, 1));
		mSoundPoolMap.put(22, mSoundPool.load(mContext, R.raw.oso, 1));
		mSoundPoolMap.put(23, mSoundPool.load(mContext, R.raw.pato, 1));
		mSoundPoolMap.put(24, mSoundPool.load(mContext, R.raw.queso, 1));
		mSoundPoolMap.put(25, mSoundPool.load(mContext, R.raw.raton, 1));
		mSoundPoolMap.put(26, mSoundPool.load(mContext, R.raw.seal, 1));
		mSoundPoolMap.put(27, mSoundPool.load(mContext, R.raw.sol, 1));
		mSoundPoolMap.put(28, mSoundPool.load(mContext, R.raw.taza, 1));
		mSoundPoolMap.put(29, mSoundPool.load(mContext, R.raw.timbre, 1));
		mSoundPoolMap.put(30, mSoundPool.load(mContext, R.raw.unepuntos, 1));
		mSoundPoolMap.put(31, mSoundPool.load(mContext, R.raw.uva, 1));
		mSoundPoolMap.put(32, mSoundPool.load(mContext, R.raw.winnie, 1));
		mSoundPoolMap.put(33, mSoundPool.load(mContext, R.raw.xilofono, 1));
		mSoundPoolMap.put(34, mSoundPool.load(mContext, R.raw.yoyo, 1));
		mSoundPoolMap.put(35, mSoundPool.load(mContext, R.raw.zapato, 1));
	}
 
	/**
	 * Plays a Sound
	 *
	 * @param index - The Index of the Sound to be played
	 * @param speed - The Speed to play not, not currently used but included for compatibility
	 */
	public static void playSound(int index)
	{
		     float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		     streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		     mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
	}
 
	/**
	 * Stop a Sound
	 * @param index - index of the sound to be stopped
	 */
	public static void stopSound(int index)
	{
		mSoundPool.stop((Integer) mSoundPoolMap.get(index));
	}
 
	/**
	 * Deallocates the resources and Instance of SoundManager
	 */
	public static void cleanup()
	{
		mSoundPool.release();
		mSoundPool = null;
	    mSoundPoolMap.clear();
	    mAudioManager.unloadSoundEffects();
	    _instance = null;
 
	}
 
}